package cn.ffcs.uec.outStaff.action;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffOrgTreeMainBean;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOutStaffOrgTreeMainComposer extends BasePortletComposer implements IPortletInfoProvider {

	private static final long serialVersionUID = 787826127352360906L;

	private Logger logger = Logger.getLogger(this.getClass());
	@Getter
	@Setter
	private UecOutStaffOrgTreeMainBean bean = new UecOutStaffOrgTreeMainBean();

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");
	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	/**
	 * 选中的组织/部门 组织与部门分别在两张表中
	 */
	private UecOrgOrgs uecOrgOrgs;
	private UecOrgDept uecOrgDept;
	
	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo = new UecOutStaffInfoVo();

	/**
	 * 组织树：组织+部门
	 */
	private OrganizationRelationVo organizationRelationVo;

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		this.bean.getUecOutStaffInfoListboxComp().setPortletInfoProvider(this);

		/**
		 * 选中内部树上的（组织关系）
		 */
		this.bean.getUecOrganizationTreeboxExt().addForward(UecOrganizationConstant.ON_SELECT_ORGREATION_TREE_REQUEST,
				this.self, "onSelectOrgReationTreeResponse");
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecOutStaffOrgTreeMainWindow() {
		
	}

	/**
	 * 选中的内部树组织关系
	 * 
	 * @param event
	 */
	public void onSelectOrgReationTreeResponse(ForwardEvent event) {
		organizationRelationVo = (OrganizationRelationVo) event.getOrigin().getData();
		if (null != organizationRelationVo) {
			logger.info("进入组织树主组件");
			this.organizationVoTransfOrgOrDept(organizationRelationVo);
			organizationRelationVo = null;
		}
	}

	/**
	 * 根据组织关系获取各对象（组织还是部门）
	 */
	public void organizationVoTransfOrgOrDept(OrganizationRelationVo organizationRelationVo) {
		uecOutStaffInfoVo.setOrgName(null);
		uecOutStaffInfoVo.setPkDept(null);
		uecOutStaffInfoVo.setPkOrg(null);
		if ("ORG".equals(organizationRelationVo.getOrgType())) {
			uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByOrganizationRelationVo(organizationRelationVo);
			// 公司
			//uecOutStaffInfoVo.setPkOrg(uecOrgOrgs.getPkOrg());
			String orgName=uecOrgOrgs.getName();
	       	 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
	       		uecOutStaffInfoVo
	       		.setOrgName(null);
	       	}else if(orgName.endsWith("*")){
	       		uecOutStaffInfoVo
	       		.setOrgName(orgName);
	       	}else {
	       		uecOutStaffInfoVo.setPkOrg(uecOrgOrgs.getPkOrg());
	       	}
		}else {
			uecOutStaffInfoVo.setPkOrg(null);
		}
		if ("DEPT".equals(organizationRelationVo.getOrgType())) {
			// 部门
			uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrganizationRelationVo(organizationRelationVo);
			// 部门所属公司
			if(uecOrgDept !=null) {
				uecOrgOrgs= new UecOrgOrgs();
				uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
				uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
				String orgName=uecOrgOrgs.getName();
				uecOutStaffInfoVo.setPkDept(uecOrgDept.getPkDept());
				uecOutStaffInfoVo.setPkOrg(uecOrgDept.getPkOrg());
				uecOutStaffInfoVo.setOrgName(orgName);
			}
		}else {
			uecOutStaffInfoVo.setPkDept(null);
		}
		Events.postEvent("onOrgTreeOutStaffQuery", this.bean.getUecOutStaffInfoListboxComp(), uecOutStaffInfoVo);
	}

}
